#!/bin/perl

open FILE, "<", "part1.xml" or die $!;
# uses part1.xml for begining part of the sample - which is occurs only once per sample.xml
open TARGET, ">", "sample.xml" or die $!;

my $rbsId=1522; 
my $startRbsNo=29; my $rbsBase="RNC73RBS";
my $lac=168; my $rac=161; my $ura=71;
my $ipLastOct=31;

my $help = "\n\n usage: ./createSample.pl sourceRnc startRbsNumber startRnsId startLac startRac startUra etIpAddressLastOctet\n sourceRnc = source RNC name\n startRbsNo = index first RBS you want to use \nstartRbsId = start id to use as first rbdId in target startLac = first lac to use\n startRac = firts rac to use\n startUra = first ura to use\n etIpAddressLastOctet = last octet of the etipAddress to be used as first ip address for field etIpAddress, the others will be a copy of this plus 1 to the last octet provided here ";

my $example = "\n\n e.g.\n ./createSample.pl RNC73 11 1001 120 160 36 1\n\n";

if ($#ARGV != 6 ) {
	print $help; print $example;
	exit;
} else {
	$srcRnc=$ARGV[0];
	$startRbsNo=$ARGV[1];
	$rbsId=$ARGV[2]; 
	$lac=$ARGV[3];
	$rac=$ARGV[4];
	$ura=$ARGV[5];
  $ipLastOct=$ARGV[6];
	$rbsBase=$srcRnc . "RBS";
}

my @lines = <FILE>;
foreach(@lines) {
  print TARGET $_;
}
close FILE;


for (my $count=0; $count<10; $count++) {
open FILE, "<", "var_rbs.xml" or die $!;
my @lines = <FILE>;
  foreach(@lines) {
  	my $orig = $_;
  	if ($orig =~ m/_RBS_ID/) {
  	  $orig =~ s/_RBS_ID/$rbsId/g;
  	  $rbsId++;
  	}
  	if ($orig =~ m/_S_RNC/) {
  	  $orig =~ s/_S_RNC/$srcRnc/g;
  	}
  	if ($orig =~ m/_RBS_NAME/) {
			if ($startRbsNo<10){
			$sourceRbsName = $rbsBase . "0" . $startRbsNo;
			} else {
			$sourceRbsName = $rbsBase . $startRbsNo;
			}
			$startRbsNo++;
  	  $orig =~ s/_RBS_NAME/$sourceRbsName/g;
  	}
  	if ($orig =~ m/_LAC/) {
  	  $orig =~ s/_LAC/$lac/g;
  	  $lac++;
  	}
  	if ($orig =~ m/_RAC/) {
  	  $orig =~ s/_RAC/$rac/g;
  	  $rac++;
  	}
  	if ($orig =~ m/_URA/) {
  	  $orig =~ s/_URA/$ura/g;
  	  $ura++;
  	}
		if ($orig =~ m/_ETIP_ADDR/) {
			$ip = "1.1.1." . $ipLastOct . "/24";
  	  $orig =~ s/_ETIP_ADDR/$ip/g;
  	  $ipLastOct++;
  	}
  	print TARGET $orig;
	}
close FILE;
}

open FILE, "<", "last.xml" or die $!;
# uses this for the last part of the xml
@lines = <FILE>;
for (@lines) {
	print TARGET $_;
}
close FILE;

close TARGET;

